package com.pig.graduate.service.impl.material;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pig.graduate.domain.finance.FinanceInfo;
import com.pig.graduate.domain.material.MaterialInventory;
import com.pig.graduate.domain.material.MaterialPig;
import com.pig.graduate.domain.material.MaterialStorage;
import com.pig.graduate.domain.sys.SysUser;
import com.pig.graduate.mapper.finance.FinanceInfoMapper;
import com.pig.graduate.mapper.material.MaterialInventoryMapper;
import com.pig.graduate.mapper.material.MaterialPigMapper;
import com.pig.graduate.mapper.sys.SysUserMapper;
import com.pig.graduate.result.MyResult;
import com.pig.graduate.service.material.MaterialStorageService;
import com.pig.graduate.mapper.material.MaterialStorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
* @author 星星眼
* @description 针对表【material_storage(物料入库)】的数据库操作Service实现
* @createDate 2023-04-02 11:57:43
*/
@Service
public class MaterialStorageServiceImpl extends ServiceImpl<MaterialStorageMapper, MaterialStorage>
    implements MaterialStorageService{

    @Autowired
    private MaterialStorageMapper materialStorageMapper;

    @Autowired
    private MaterialInventoryMapper materialInventoryMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private FinanceInfoMapper financeInfoMapper;

    @Autowired
    private MaterialPigMapper materialPigMapper;

    @Override
    public MyResult storageList(MaterialStorage materialStorage) {
        PageHelper.startPage(materialStorage.getPageNum(),materialStorage.getPageSize());
        List<MaterialStorage> list = materialStorageMapper.storageList(materialStorage);
        PageInfo<MaterialStorage> pageInfo = new PageInfo<>(list);
        return MyResult.success(pageInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public MyResult addMaterialStorage(MaterialStorage materialStorage) {
        materialStorage.setState(0);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            String currentUserName = authentication.getName();
            SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("username", currentUserName));
            materialStorage.setOperatorsUser(sysUser.getId());
        }
        materialStorage.setSdNumber(materialStorage.getStorageNumber());
        materialStorage.setTotalPrice(materialStorage.getBatchPrice().multiply(new BigDecimal(materialStorage.getStorageNumber())));
        materialStorageMapper.insert(materialStorage);
        QueryWrapper<MaterialInventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("material_id",materialStorage.getMaterialId());
        MaterialInventory materialInventory = materialInventoryMapper.selectOne(queryWrapper);
        if(materialInventory == null){
            MaterialInventory inventory = new MaterialInventory();
            inventory.setMaterialId(materialStorage.getMaterialId());
            inventory.setInventoryNumber(materialStorage.getStorageNumber());
            inventory.setTotalPrice(materialStorage.getTotalPrice());
            materialInventoryMapper.insert(inventory);
        }else{
            materialInventory.setInventoryNumber(materialInventory.getInventoryNumber()+materialStorage.getStorageNumber());
            materialInventory.setTotalPrice(materialInventory.getTotalPrice().add(materialStorage.getTotalPrice()));
            materialInventoryMapper.updateById(materialInventory);
        }
        //财务数据
        FinanceInfo financeInfo = new FinanceInfo();
        financeInfo.setName("物料购买");
        financeInfo.setCreateTime(new Date());
        financeInfo.setPrice(materialStorage.getBatchPrice().multiply(new BigDecimal(-1)));
        financeInfo.setTotalPrice(materialStorage.getTotalPrice().multiply(new BigDecimal(-1)));
        MaterialPig materialPig = materialPigMapper.selectById(materialStorage.getMaterialId());
        financeInfo.setSummary(materialPig.getName());
        financeInfo.setNumber(materialStorage.getStorageNumber());
        financeInfo.setDataType("物料支出");
        financeInfo.setIncomeExpensesType("支出");
        financeInfoMapper.insert(financeInfo);
        return MyResult.success("入库成功");
    }

    @Override
    public MyResult expirationReminders(MaterialStorage materialStorage) {
        PageHelper.startPage(materialStorage.getPageNum(),materialStorage.getPageSize());
        List<MaterialStorage> list = materialStorageMapper.getMaterialsOverdue(materialStorage);
        PageInfo<MaterialStorage> pageInfo = new PageInfo<>(list);
        return MyResult.success(pageInfo);
    }

    @Override
    public List<MaterialStorage> getMaterialsNoOverdue(){
        return materialStorageMapper.getMaterialsNoOverdue();
    }
}




